package com.assets.query;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.assets.BaseActivity;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.ShowBitmapActivity;
import com.assets.databinding.ActivityQueryDetailBinding;
import com.assets.newNHF.util.LogUtils;
import com.assets.room.database.MyDataBase;
import com.assets.util.GetImgFromAlbum;
import com.assets.util.MyOkHttp;
import com.bumptech.glide.Glide;
import com.google.gson.JsonIOException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

import okhttp3.Response;

/**
 * Created by Administrator on 2018/12/26.
 */

public class QueryDetailActivity extends BaseActivity {
    private ActivityQueryDetailBinding binding;
    private String uploadAsset = "http://" + Url.IP + "/api/uploadAsset";
    private String assetNumber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityQueryDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ImageView fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(v -> {
            finish();
        });
        binding.tvTitle.rfidSet.setVisibility(View.GONE);
        binding.tvTitleAssets.tvTitle1.setText("资产编号");//资产编号
        binding.tvTitleAssets.tvTitle2.setText("资产名称");//资产名称
        binding.tvTitleManager.tvTitle1.setText("入账日期");//管理部门
        binding.tvTitleManager.tvTitle2.setText("管理员");//管理员
        binding.tvTitleCompany.tvTitle1.setText("所属地点");//公司
        binding.tvTitleCompany.tvTitle2.setText("品牌");//事业部
        binding.tvTitleUse.tvTitle1.setText("使用部门");//使用部门
        binding.tvTitleUse.tvTitle2.setText("使用人");//使用人
        binding.tvTitleModelCategory.tvTitle1.setText("型号");//型号
        binding.tvTitleModelCategory.tvTitle2.setText("类别");//类别
        binding.tvTitleStatusMode.tvTitle1.setText("资产状态");//资产状态
        binding.tvTitleStatusMode.tvTitle2.setText("机柜");//获取方式
        binding.tvTitleYearJingcan.tvTitle1.setText("使用年限");//使用年限
        binding.tvTitleYearJingcan.tvTitle2.setText("机房");//净残值
        binding.tvTitleUnitAmount.tvTitle1.setText("备注");//单位
        binding.tvTitleUnitAmount.tvTitle2.setText("金额");//金额
//        binding.tvTitleAddressRegion.tvTitle1.setText("机柜");//存放地方
//        binding.tvTitleAddressRegion.tvTitle2.setText("地区");//地区
//        binding.tvTitleBrandSupplier.tvTitle1.setText("品牌");//品牌
//        binding.tvTitleBrandSupplier.tvTitle2.setText("供应商");//供应商
//        binding.tvTitleDateRemark.tvTitle1.setText("入账日期");//入账日期
//        binding.tvTitleDateRemark.tvTitle2.setText("备注");//备注
        Intent intent = getIntent();
        QueryData data = intent.getParcelableExtra("queryData");
        setData(data);
        binding.up.setOnClickListener(v -> {
            openAlbum();
        });
    }

    @Override
    public int intiLayout() {
        return -1;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
    }

    private void setData(QueryData queryData) {
        runOnUiThread(() -> {
            String image = queryData.getAsset().getImage();
            if (!TextUtils.isEmpty(image)) {
                //image = "http://" + Url.IP + image;
                Glide.with(this).load(image).into(binding.iv);
                String finalImage = image;
                binding.iv.setOnClickListener(v -> {
                    showBitmap(finalImage);
                });
            } else Glide.with(this).load(R.mipmap.null_po).into(binding.iv);
            assetNumber = queryData.getAsset().getAssetNumber();//资产编号
            binding.tvContentAssets.tvContent1.setText(queryData.getAsset().getAssetNumber().isEmpty() ? "暂无资产编号":queryData.getAsset().getAssetNumber());//资产编号
            binding.tvContentAssets.tvContent2.setText(queryData.getAsset().getAssetName().isEmpty() ? "暂资产名称":queryData.getAsset().getAssetName());//资产名称
            binding.tvContentManager.tvContent1.setText(queryData.getAsset().getCreatedate().isEmpty() ? "暂无入账日期":queryData.getAsset().getCreatedate()/*queryData.getAsset().getDeptId().isEmpty() ? "入账日期":queryData.getAsset().getDeptId()*/);//管理部门
            binding.tvContentManager.tvContent2.setText(MyDataBase.init(QueryDetailActivity.this).userDao().getNameByIds(queryData.getAsset().getAdmin()) == null/*queryData.getAsset().getAdmin().isEmpty()*/ ? "暂无管理员":MyDataBase.init(QueryDetailActivity.this).userDao().getNameByIds(queryData.getAsset().getAdmin()));//管理员
            binding.tvContentCompany.tvContent1.setText(queryData.getAsset().getCompany()/*MyDataBase.init(QueryDetailActivity.this).placeDao().getNameByPid(MyDataBase.init(QueryDetailActivity.this).placeDao().getAllById(queryData.getAsset().getPlace()).getPid()) == null*//*queryData.getAsset().getCompany().isEmpty()*//* ? "暂无所属地点":MyDataBase.init(QueryDetailActivity.this).placeDao().getNameByPid(MyDataBase.init(QueryDetailActivity.this).placeDao().getAllById(queryData.getAsset().getPlace()).getPid())*/);//公司
            binding.tvContentCompany.tvContent2.setText(queryData.getAsset().getBrand().isEmpty() ? "暂无品牌":queryData.getAsset().getBrand());//事业部 改成品牌
            binding.tvContentUse.tvContent1.setText(MyDataBase.init(QueryDetailActivity.this).deptDao().getNameById(queryData.getAsset().getUserDept()) ==null/*queryData.getAsset().getUserDept().isEmpty()*/ ? "暂无使用部门":MyDataBase.init(QueryDetailActivity.this).deptDao().getNameById(queryData.getAsset().getUserDept()));//使用部门
            binding.tvContentUse.tvContent2.setText(queryData.getAsset().getUserId().isEmpty() ? "暂无使用人": MyDataBase.init(QueryDetailActivity.this).userDao().getNameByIds(queryData.getAsset().getUserId()) == null ? "暂无使用人" : MyDataBase.init(QueryDetailActivity.this).userDao().getNameByIds(queryData.getAsset().getUserId()));//使用人
            binding.tvContentModelCategory.tvContent1.setText(queryData.getAsset().getModel().isEmpty() ? "暂无型号":queryData.getAsset().getModel());//型号
            binding.tvContentModelCategory.tvContent2.setText(queryData.getAsset().getType().isEmpty() ? "暂无类别":queryData.getAsset().getType());//类别

            if ("1".equals(queryData.getAsset().getStatus())) {
                binding.tvContentStatusMode.tvContent1.setText("在用");
            }else if ("2".equals(queryData.getAsset().getStatus())) {
                binding.tvContentStatusMode.tvContent1.setText("报废");
            }
            else if ("4".equals(queryData.getAsset().getStatus())) {
                binding.tvContentStatusMode.tvContent1.setText("闲置");
            }
//            binding.tvContentStatusMode.tvContent1.setText(queryData.getAsset().getStatus().isEmpty() ? "暂无资产状态":queryData.getAsset().getStatus());//资产状态

//            binding.tvContentStatusMode.tvContent1.setText(queryData.getAsset().getStatus().isEmpty() ? "暂无资产状态":queryData.getAsset().getStatus());//资产状态
            binding.tvContentStatusMode.tvContent2.setText(queryData.getAsset().getAccountant()/*queryData.getAsset().getAccountant()*/ == null || queryData.getAsset().getAccountant().isEmpty()?"暂无机柜":queryData.getAsset().getAccountant()/*queryData.getAsset().getGrundlagen().isEmpty() ? "暂无获取方式":queryData.getAsset().getGrundlagen()*/);//获取方式
            binding.tvContentYearJingcan.tvContent1.setText(queryData.getAsset().getYear().isEmpty() ? "暂无使用年限":queryData.getAsset().getYear());//使用年限
            binding.tvContentYearJingcan.tvContent2.setText(/*queryData.getAsset().getSalvage()*/queryData.getAsset().getFinancialCode().isEmpty() ? "暂无机房":queryData.getAsset().getFinancialCode());//净残值
            binding.tvContentUnitAmount.tvContent1.setText(queryData.getAsset().getUnit().isEmpty() ? "暂无备注":queryData.getAsset().getRemark());//单位
            binding.tvContentUnitAmount.tvContent2.setText(queryData.getAsset().getAmount().isEmpty() ? "暂无金额":queryData.getAsset().getAmount());//金额
//            binding.tvContentAddressRegion.tvContent1.setText(/*queryData.getAsset().getPlace()*/queryData.getAsset().getAccountant() == null ?"暂无机柜":queryData.getAsset().getAccountant());//存放地方
//            binding.tvContentAddressRegion.tvContent2.setText(queryData.getAsset().getArea().isEmpty() ? "暂无地区":queryData.getAsset().getArea());//地区
//            binding.tvContentBrandSupplier.tvContent1.setText(queryData.getAsset().getBrand());//品牌
//            binding.tvContentBrandSupplier.tvContent2.setText(queryData.getAsset().getSupplier());//供应商
//            binding.tvContentDateRemark.tvContent1.setText(queryData.getAsset().getCreatedate().isEmpty() ? "暂无入账日期":queryData.getAsset().getCreatedate());//入账日期
//            binding.tvContentDateRemark.tvContent2.setText(queryData.getAsset().getRemark().isEmpty() ? "暂无备注":queryData.getAsset().getRemark());//备注
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 55 && resultCode == RESULT_OK) {
            Uri uri;
            uri = data.getData();
            String path = GetImgFromAlbum.getRealPathFromUriAboveApi19(this, uri);
            LogUtils.d(path);
            File file = new File(path);
            postBitmap(uploadAsset, file, assetNumber, responseData -> {
                LogUtils.d(responseData);
                try {
                    String state = new JSONObject(responseData).optString("state");
                    if (TextUtils.isEmpty(state) || !state.equals("1")) {
                        showToast("上传失败");
                    } else {
                        runOnUiThread(() -> {
                            showToast("上传成功");
                            Glide.with(this).load(file).into(binding.iv);
                            binding.iv.setOnClickListener(v -> {
                                showBitmap(file);
                            });
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {//2000毫秒
                //大于2000ms则认为是误操作，使用Toast进行提示
                showToast("再按一次返回");
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();//上次点击时间
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
